# -*- coding: utf-8 -*-
'''
Created on Apr 2, 2013

@author: LONG HOANG GIANG
'''

import pyLib, os
import json, re, traceback
from urlparse import urljoin


def getDetail(item):
    print '> getDetail {0}'.format(item['url'])
    outputPath = "/longhoanggiang/truyentext/yeunhamchihaiduocnhamemgai/files"
    tree = pyLib.loadWeb(item['url'], cached=True).build_tree()
    contentNode = tree.xpath("//div[@id='content']")[0]
    
    pyLib.Etree.clean(contentNode.xpath(".//font[@color='red']/strong/.."))
    pyLib.Etree.cleanNodeNextSibling(contentNode.xpath(".//div[@id='”fb-root”']".decode('utf-8')), True)
    htmlStr = pyLib.cleanHTML(pyLib.Etree.tostring(contentNode), ['b', 'strong'])
    print htmlStr
    htmlStr = re.sub(r"\n", "<br />", htmlStr)
    htmlStr = '''<b>{0}</b><br /><hr /><br />{1}'''.format(item['title'], htmlStr)
    filePath = outputPath + "/" + item['id']
    pyLib.gzip(filePath, json.dumps(htmlStr))

def getCategories(url):
    
    outputPath = "/longhoanggiang/truyentext/yeunhamchihaiduocnhamemgai/files"
    pyLib.createIfNotExistsPath(outputPath)
    
    data = []
    
    while url != '':
        tree = pyLib.loadWeb(url).build_tree()
        for item in tree.xpath("//div[@id='sidebar']//li/a"):
            try :
                name = pyLib.stringify(item)
                if name in ['Danh mục hiện tại', 'Thông báo', 'Hướng dẫn'] or name == '': continue
                link = item.get('href', '').strip()
                if link == '': continue
                print link
                link = urljoin(url, link)
                fileName = pyLib.crc32unsigned(link + name)
                getDetail({'title': name, 'id': fileName, 'url': link})
                data.append({'title': name, 'id': fileName})
            except:
                traceback.print_exc()
                pass
        nextNode = tree.xpath("//div[@id='sidebar']//li/a/img[contains(@src, 'next.png')]")
        if len(nextNode) == 0: break
        link = nextNode[0].getparent().get('href')
        if link == '': break
        url = urljoin(url, link)
    
    pyLib.gzip(os.path.dirname(outputPath) + "/data", json.dumps(data))
    
    
if __name__ == '__main__':
    
    url = 'http://www.yeunhamchihaiduocnhamemgai.com/danh-muc.php'
    
    getCategories(url)
    
    print '> Finished'
    os._exit(1)